package question18;

public class question18 {

    public String maxString(String s) {
        boolean[][] dp = new boolean[s.length()][s.length()];

        for (int i = 0; i < s.length(); i++) {
            dp[i][i] = true;
        }

        int maxLen = 0;
        String maxString = "";

        for (int i = s.length() - 1; i >= 0; i--) {
            for (int j = i; j < s.length(); j++) {
                if (s.charAt(i) == s.charAt(j)) {
                    if (j - i < 2) {
                        dp[i][j] = true;
                    } else {
                        dp[i][j] = dp[i+1][j-1];
                    }
                }
                if (dp[i][j] && (j - i + 1) > maxLen) {
                    maxLen = j - i + 1;
                    maxString = s.substring(i, j + 1);
                }
            }
        }

        return maxString;
    }

    public static void main(String[] args) {
        String s = new question18().maxString("babad");
        System.out.println(s);
    }
}
